package com.wedding.app.controller;

import com.wedding.app.entity.User;
import com.wedding.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户登录
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();
        String username = params.get("username");
        String password = params.get("password");
        User user = userService.login(username, password);
        if (user != null) {
            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("data", user);
            // 实际项目中应该生成并返回token
            result.put("token", "fake-token-for-demo");
        } else {
            result.put("code", 400);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

    // 邮箱验证码登录
    @PostMapping("/loginByEmail")
    public Map<String, Object> loginByEmail(@RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();
        String email = params.get("email");
        String code = params.get("code");
        User user = userService.loginByEmail(email, code);
        if (user != null) {
            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("data", user);
            // 实际项目中应该生成并返回token
            result.put("token", "fake-token-for-demo");
        } else {
            result.put("code", 400);
            result.put("message", "邮箱或验证码错误");
        }
        return result;
    }

    // 发送验证码
    @PostMapping("/sendCode")
    public Map<String, Object> sendCode(@RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();
        String email = params.get("email");
        boolean success = userService.sendCode(email);
        if (success) {
            result.put("code", 200);
            result.put("message", "验证码发送成功");
        } else {
            result.put("code", 400);
            result.put("message", "验证码发送失败");
        }
        return result;
    }

    // 用户注册
    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();
        String username = params.get("username");
        String password = params.get("password");
        String email = params.get("email");
        String code = params.get("code");
        
        // 检查用户名是否已存在
        if (userService.selectByUsername(username) != null) {
            result.put("code", 400);
            result.put("message", "用户名已存在");
            return result;
        }
        // 检查邮箱是否已存在
        if (userService.selectByUsername(email) != null) {
            result.put("code", 400);
            result.put("message", "邮箱已被注册");
            return result;
        }
        // 验证验证码
        if (!userService.verifyCode(email, code)) {
            result.put("code", 400);
            result.put("message", "验证码错误");
            return result;
        }
        // 创建用户对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(email); // 临时存储邮箱到phone字段，实际项目中应该添加email字段
        
        int count = userService.insert(user);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "注册成功");
            result.put("success", true);
        } else {
            result.put("code", 400);
            result.put("message", "注册失败");
            result.put("success", false);
        }
        return result;
    }

    // 密码找回
    @PostMapping("/findPassword")
    public Map<String, Object> findPassword(@RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();
        String email = params.get("email");
        String code = params.get("code");
        String password = params.get("password");
        // 验证验证码
        if (userService.verifyCode(email, code)) {
            User user = userService.selectByUsername(email);
            if (user != null) {
                int count = userService.updatePassword(user.getId(), password);
                if (count > 0) {
                    result.put("code", 200);
                    result.put("message", "密码重置成功");
                } else {
                    result.put("code", 400);
                    result.put("message", "密码重置失败");
                }
            } else {
                result.put("code", 400);
                result.put("message", "用户不存在");
            }
        } else {
            result.put("code", 400);
            result.put("message", "验证码错误");
        }
        return result;
    }

    // 获取用户信息
    @GetMapping("/info/{id}")
    public Map<String, Object> getUserInfo(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        User user = userService.selectById(id);
        if (user != null) {
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", user);
        } else {
            result.put("code", 400);
            result.put("message", "用户不存在");
        }
        return result;
    }

    // 修改用户信息
    @PutMapping("/update")
    public Map<String, Object> updateUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        int count = userService.update(user);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "修改成功");
        } else {
            result.put("code", 400);
            result.put("message", "修改失败");
        }
        return result;
    }

    // 修改用户状态
    @PutMapping("/status/{id}/{status}")
    public Map<String, Object> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Map<String, Object> result = new HashMap<>();
        int count = userService.updateStatus(id, status);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "状态修改成功");
        } else {
            result.put("code", 400);
            result.put("message", "状态修改失败");
        }
        return result;
    }

    // 查询所有用户
    @GetMapping("/list")
    public Map<String, Object> getUserList() {
        Map<String, Object> result = new HashMap<>();
        List<User> userList = userService.selectAll();
        result.put("code", 200);
        result.put("message", "获取成功");
        result.put("data", userList);
        return result;
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteUser(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        int count = userService.deleteById(id);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 400);
            result.put("message", "删除失败");
        }
        return result;
    }
}